package hotel.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import hotel.dao.DaoManager;
import hotel.dao.RoomDao;
import hotel.dao.base.ConnectionBehavior;
import hotel.dao.base.DaoCommand;
import hotel.entity.RoomEntity;
import hotel.model.IRoom;
import hotel.model.Room;
import hotel.service.RoomService;

public class RoomServiceImpl implements RoomService 
{
	private static final Logger logger = Logger.getLogger(UserServiceImpl.class);
	private static final String ROOMSERVICEIMPL_ERROR = "Error in RoomServiceImpl:";
	
	private DaoManager daoManager;
	
	public RoomServiceImpl(DaoManager daoManager) 
	{
		this.daoManager = daoManager;
	}
	
	protected DaoManager getDaoManager() 
	{
		return daoManager;
	}

	@Override
	public List<IRoom> getRoomByRoomClass(String roomClass)
	{		// TODO For Main Page
		return null;
	}

	@Override
	public List<IRoom> getRoomByBeds(int beds)
	{		// TODO For Main Page
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<IRoom> getRoomByRoomClassAndBeds(final int beds, final String roomClass)
	{		
		return (List<IRoom>)
				getDaoManager().command(new DaoCommand()
					{
						public Object execute(final DaoManager manager) 
						{
							final RoomDao roomDao = manager.getDao(RoomDao.class);
					    	List<RoomEntity> listRoomEntity = null;
					    	try
					    	{
					    		listRoomEntity = roomDao.findRoomByRoomClassAndBeds(beds, roomClass);
					    	}
					    	catch (SQLException e) 
					    	{
					    		logger.error(ROOMSERVICEIMPL_ERROR + e);
					    	}
					  		
					    	final List<IRoom> listRoom = new ArrayList<IRoom>();
					    	for (final RoomEntity roomEntity : listRoomEntity) 
					    	{
					    		listRoom.add(convertToModel(roomEntity));
					    	}
			  		
					    	return listRoom;
						}
					}, ConnectionBehavior.CLOSE);

	}

	
	@SuppressWarnings("unchecked")
	@Override
	public List<IRoom> getRooms() 
	{
		return (List<IRoom>)
		getDaoManager().command(new DaoCommand()
			{
				public Object execute(final DaoManager manager) 
				{
					final RoomDao roomDao = manager.getDao(RoomDao.class);
			    	List<RoomEntity> listRoomEntity = null;
			    	try
			    	{
			    		listRoomEntity = roomDao.findAll();
			    	}
			    	catch (SQLException e) 
			    	{
			    		logger.error(ROOMSERVICEIMPL_ERROR + e);
			    	}
			  		
			    	final List<IRoom> listRoom = new ArrayList<IRoom>();
			    	for (final RoomEntity roomEntity : listRoomEntity) 
			    	{
			    		listRoom.add(convertToModel(roomEntity));
			    	}
	  		
			    	return listRoom;
				}
			}, ConnectionBehavior.CLOSE);

	}

	@Override
	public void insert(final String roomClass, final int beds) 
	{
		getDaoManager().command(new DaoCommand()
		{
			public Object execute(final DaoManager manager) 
			{
				final RoomDao roomDao = manager.getDao(RoomDao.class);
		    	try
		    	{
		    		roomDao.insert(new RoomEntity(0, roomClass, beds, null, null, 0));
		    	}
		    	catch (Exception e) 
		    	{
		    		logger.error(ROOMSERVICEIMPL_ERROR + e);
		    	}
		    	return null;
			}
		}, ConnectionBehavior.CLOSE);
	}
	
	public void deleteRoom(final int id)
	{
		getDaoManager().command(new DaoCommand()
			{
				public Object execute(final DaoManager manager) 
				{
					final RoomDao roomDao = manager.getDao(RoomDao.class);
			    	try
			    	{
			    			roomDao.delete(id);
			    	}
			    	catch (SQLException e) 
			    	{
			    		logger.error(ROOMSERVICEIMPL_ERROR + e);
			    	} catch (Exception e) {
			    		logger.error(ROOMSERVICEIMPL_ERROR + e);
					}
			    	
			    	return null;
				}
			}, ConnectionBehavior.CLOSE);	
	}

	@Override
	public void updateRoom(final int roomId, final int requestId) 
	{
		getDaoManager().command(new DaoCommand()
		{
			public Object execute(final DaoManager manager) 
			{
				final RoomDao roomDao = manager.getDao(RoomDao.class);
		    	try
		    	{
		    		roomDao.updateRoom(roomId, requestId);
		    	}
		    	catch (Exception e) 
		    	{
		    		logger.error(ROOMSERVICEIMPL_ERROR + e);
		    	}
		    	return null;
			}
		}, ConnectionBehavior.CLOSE);

	}
	
	@Override
	public IRoom getById(final int id)
	{
		return (IRoom)
				getDaoManager().command(new DaoCommand()
					{
						public Object execute(final DaoManager manager) 
						{
							final RoomDao roomDao = manager.getDao(RoomDao.class);
					    	RoomEntity roomEntity = null;
					    	try
					    	{
					    		roomEntity = roomDao.findById(id);
					    	}
					    	catch (SQLException e) 
					    	{
					    		logger.error(ROOMSERVICEIMPL_ERROR + e);
					    	}
					  		
					    	IRoom room = null;
					    		room = convertToModel(roomEntity);
			  		
					    	return room;
						}
					}, ConnectionBehavior.CLOSE);
	}

	public void dismissRoom(final int id)
	{
		getDaoManager().command(new DaoCommand()
		{
			public Object execute(final DaoManager manager) 
			{
				final RoomDao roomDao = manager.getDao(RoomDao.class);
		    	try
		    	{
		    		roomDao.dismissRoom(id);
		    	}
		    	catch (SQLException e) 
		    	{
		    		logger.error(ROOMSERVICEIMPL_ERROR + e);
		    	}
		    	return null;
			}
		}, ConnectionBehavior.CLOSE);

	}
	
	private IRoom convertToModel(RoomEntity roomEntity) 
	{
	IRoom room = new Room(
					roomEntity.getId(),
					roomEntity.getRoomClass(),
					roomEntity.getBeds(),
					roomEntity.getName(),
					roomEntity.getSurname(),
					roomEntity.getUserID());
	return room;
	}

}
